<?php
/*
This sends a little bit of JSON to say if the user is logged in. 
*/

require_once( 'core.php' );
require_once( 'current_user_api.php' );
require_once( 'bug_api.php' );


auth_ensure_user_authenticated( );
//helper_begin_long_process( );

$t_plugin_path = config_get( 'plugin_path' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'TableMetadata.php' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'JSONstore.php' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'MetadataStandards.php' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'ProjectsMetadata.php' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'HTTPclient.php' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'TableOutputs.php' );

/*
 Turn this on to update the metadata standards
 
$MetadataStandards = new MetadataStandards;
$MetadataStandards->saveMetadata();
$MetadataStandards->datasetMetadata();
*/



$authHashPrefix = plugin_config_get( 'authHashPrefix' );
$refineHumanURI = plugin_config_get( 'refineBaseURI' ); //for human linking
$refineURI = plugin_config_get( 'refineLocalURI' );
if(!$refineURI){
	$refineURI = plugin_config_get( 'refineBaseURI' );
}

# extracts the user information for the currently logged in user
# and prefixes it with u_
$f_user_id = gpc_get_int( 'id', auth_get_current_user_id() );
$row = user_get_row( $f_user_id );

extract( $row, EXTR_PREFIX_ALL, 'u' );
$u_email = user_get_email( $u_id );
$u_realname = user_get_realname( $u_id );






if(!isset($_GET["grProject"])){
	$GRprojectID =  false;
}
else{
	$GRprojectID = $_GET["grProject"]; 
}

if(isset($_GET["test"])){
	$reconTesting = "test";
}
else{
	$reconTesting = false;
}



$tableMetaObj = new TableMetadata;
$tableMetaObj->refineURI = $refineURI;
$tableMetaObj->refineHumanURI = $refineHumanURI;
$tableMetaObj->authHashPrefix = $authHashPrefix;
$tableMetaObj->currentUserID = $f_user_id;
$tableMetaObj->currentUserName = $u_username;
$tableMetaObj->GRprojectID = $GRprojectID;
$tableMetaObj->get_metadata();

$tableMetaObj->count_description_done(); //count number of completed general descriptive metadata fields;
$tableMetaObj = check_edit_resolution($tableMetaObj); //now check on the status of the edit issue
$tableMetaObj = check_metadata_fieldDes_resolution($tableMetaObj); //now check on status of the metadata desription and field documentation issues
$tableMetaObj = check_otherIssues_resolution($tableMetaObj); //finally check on the status of the "other issues"

$tableMetaObj->count_description_done();
$tableMetaObj->count_column_documentation();
$tableMetaObj->count_otherIssue_done(); //count other issues, and number resolved
$tableMetadata = $tableMetaObj->currentMetadata;

if(!check_dataset_projectID_auth($tableMetadata)){
	break; //not authorized to view, stop showing anything else!
}


$ServiceLinks = array("EOL" => array("href" => "javascript:openReconDialogue('EOL');",
				     "title"=> "Select a column to reconcile with the EOL" ),
		      
		      "Pleiades" => array("href" => "javascript:openReconDialogue('Pleiades');",
				     "title"=> "Select a column to reconcile with the Pleiadees" )
		      );




html_page_top( "Entity Reconciliation for Linking Data" );

?>

<div class="data-doc-page-body">
	
	<!-- page heading information -->
	<div class="data-doc-page-heading">
		<div class="data-doc-page-heading-row">
			<div class="data-doc-page-heading-title">
				<h2>Dataset Linking and Entity Reconciliation <?php if($reconTesting != false){ echo "(Testing)";} ?></h2>
				<p>Issues and Summary Status: [<?php echo "<a href=\"../plugin.php?page=DataRefine/data-doc-summary&grProject=".$GRprojectID."\">Link</a>"; ?>]</p>
				<br/>
				<span style="font-size:smaller;">Additional Tips and How-to's on linking data in Google Refine [<a href="../plugin.php?page=DataRefine/data-doc-recipes">Useful Tips</a>]</span>
			</div>
			<div class="data-doc-page-heading-dset">
				<div class="data-doc-page-heading-dset-tab">
					<div class="data-doc-page-heading-dset-row">
						<div class="data-doc-page-heading-dset-title">
							<?php
							if(isset($tableMetadata["description"][0]["values"][0]["text"])){
								$useTableName = $tableMetadata["description"][0]["values"][0]["text"];
								echo "<h3>&ldquo;".$tableMetadata["description"][0]["values"][0]["text"]."&rdquo;</h3>";
								echo "<br/><br/><span class=\"dest-sub-title\">(In refine as: <em>".$tableMetadata["name"]."</em>)</span>";	
							}
							else{
								$useTableName = $tableMetadata["name"];
								echo "<h3>&ldquo;".$tableMetadata["name"]."&rdquo;</h3>";
							}
							
							?>
							
						</div>
						<div class="data-doc-page-heading-dset-des">
						This table currently has <span id="dataset-row-count"><?php echo $tableMetadata["refineRowCount"];  ?></span> rows. The last edit / modification of this table in Google Refine took place on <span id="dataset-last-modified"><?php echo date("F j, Y, g:i a", strtotime($tableMetadata["refineModified"]));  ?></span>. View the dataset in Google refine: [<a href="<?php echo $refineHumanURI."/project?project=".$GRprojectID;  ?>">Here</a>]
						<br/><br/>
						Download dataset metadata and documentation: [<a href="<?php echo "/plugins/DataRefine/metadata/".$GRprojectID.".json";  ?>">JSON-format</a>]
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>

	<!-- Main content for this dataset tracking page -->	
	<div class="data-doc-page-primary">
		<h3>One-Click Entity Reconciliation</h3>
		<p>Google Refine is a powerful tool for reviewing and cleaning data. Linking datasets to shared vocabularies and concepts
		published on the Web can help make datasets even more useful to more people. Google Refine has several features that enable
		users to relate and link data to different Web collections through a semi-automated process of <em>entity reconciliation</em> [see
		<a href="http://wiki.freebase.com/wiki/Reconciliation" title="Freebase introduction to Entity Reconcilation">explaination</a>].
		</p>
		<p>Google Refine's entity reconcilation features are easiest to use when linking data to
		<a href="http://www.freebase.com/">Freebase</a>. However, the process for linking data in Google Refine to other Web-based
		collections is somewhat more involved. Since many researchers may need to relate data to collections other than Freebase,
		Data Refine provides a "one-click" reconciliation options for the collections described below.</p>
		
		<p>By referencing shared concepts, datasets become more intelligible and better contextualized. Please note however that
		you should use Google Refine to <strong>check and edit the results</strong> of entity reconciliation services.</p>
		<br/>
		<h4>Supported Reconcilation Services</h4>
		
		<input type="hidden" id="GRprojectID" value="<?php echo $GRprojectID; ?>" />
		<input type="hidden" id="RefineProjURL" value="<?php echo $refineHumanURI."/project?project=".$GRprojectID; ?>" />
		<input type="hidden" id="reconTesting" value="<?php echo $reconTesting; ?>" />
		
		<table>
			<thead>
				<tr>
					<th>Web-Based Collection</th>
					<th>Applications</th>
					<th>Reconcile with this Collection</th>
				</tr>
			</thead>
			<tbody>
				<tr class="rec-row-a">
					<td class="rec-col-repository">
						Encyclopedia of Life (EOL)
						<br/>
						<a href="http://eol.org">http://eol.org</a>
					</td>
					<td class="rec-col-exp">
						The EOL maintains a database of biological taxa that
						reconciles several taxonomic systems. Each taxonomic unit has
						it's own Web page, identified with a stable URI. Reference to
						these URIs can add precision to taxonomic identification of
						contributed datasets.
					</td>
					<td class="rec-col-howto">
						<?php
						
						echo "[<a href=\"".$ServiceLinks["EOL"]["href"]."\" title=\"".$ServiceLinks["EOL"]["title"]."\">";
						echo "Choose a column in '$useTableName' to reconcile with EOL";
						echo "</a>]";
						
						?>
					</td>
				</tr>
				<tr class="rec-row-b">
					<td class="rec-col-repository">
						Pleiades
						<br/>
						<a href="http://pleiades.stoa.org/">http://pleiades.stoa.org/</a>
					</td>
					<td class="rec-col-exp">
						Pleiades is a gazetteer of ancient places, mainly focused on the Mediterranean world
						and adjacent regions. Each place has it's own Web page, identified with a stable URI. Reference to
						these URIs can add precision to place identifications datasets. Several digital initiatives for
						archaeology and ancient history reference Pleaides places, enabling increasingly sophisticated
						applications of Linked Open Data for studies of the ancient world
						[see example, <a href="http://pelagios-project.blogspot.com/">Pelagios</a>].
					</td>
					<td class="rec-col-howto">
						<?php
						
						echo "[<a href=\"".$ServiceLinks["Pleiades"]["href"]."\" title=\"".$ServiceLinks["Pleiades"]["title"]."\">";
						echo "Choose a column in '$useTableName' to reconcile with Pleiades";
						echo "</a>]";
						
						?>
					</td>
				</tr>
			</tbody>
		</table>
		
		<div id="paneArea" >
		</div>
		
	</div>
</div>

	

<?php
html_page_bottom();
?>
